package com.kotlin.flong.coroutines

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlin.system.measureTimeMillis

/**
 * @Author:liangjl
 * @Date: 2020/7/7-20:25
 * @Eamil:jilongliang@sina.com
 * @Description:
 * 异步风格的函数,kotlin官方不建议使用此风格进行编程
 */
fun main() {
    var measure = measureTimeMillis {

        var value1 = intValueAsync1()
        var value2 = intValueAsync2()

        runBlocking {
            println("result ${value1.await() + value2.await()}")

        }
    }

    println("总耗时：$measure")
}

fun intValueAsync1() = GlobalScope.async {
    intValue1()
}

fun intValueAsync2() = GlobalScope.async {
    intValue2()
}

private suspend fun intValue1(): Int {
    delay(2000)
    return 15
}

private suspend fun intValue2(): Int {
    delay(3000)
    return 20
}